﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Xml.Linq;

namespace WOS.Plugins.Remote
{
    public class Command
    {
        public string Name { get; set; }
        public object Data { get; set; }
    }
    public class RemoteManager
    {
        #region "Policy"
        public int PolicyPort
        {
            get { return 943; }
        }
        public string PolicyFile
        {
            get { return "policy.xml"; }
        }
        public int PolicyBufferSize
        {
            get { return 1024; }
        }
        public void StartPolicy()
        {
            byte[] fileBytes = File.ReadAllBytes(PolicyFile);
            Socket connectSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connectSock.Bind(new IPEndPoint(IPAddress.Any, PolicyPort));
            connectSock.Listen(5);
            byte[] buffer = new byte[PolicyBufferSize];
            while (true)
            {
                Socket incomingSocket = connectSock.Accept();
                incomingSocket.Receive(buffer);
                incomingSocket.Send(fileBytes, 0, fileBytes.Length, SocketFlags.None);
                incomingSocket.Close();
            }
        }
        #endregion

        #region "Trace"
        public int TracePort
        {
            get { return 4502; }
        }
        public void Trace()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Any, TracePort));
            socket.Listen(5);

            while (true)
            {
                Socket s = socket.Accept();

                byte[] buffer = new byte[1024];
                int received = s.Receive(buffer);
                if (received != 0)
                {
                    //bu kısmı aktif yağtığımda hata alıyorum

                    string commandData = Encoding.Unicode.GetString(buffer, 0, received);
                    // <Command name="AddFile">http://www.google.com/</Command>
                    CommandEventArgs e = new CommandEventArgs();
                    //Whe.Serialization.Serializer se = new Whe.Serialization.Serializer();
                    //e.Client = new RemoteClient();
                    //e.Client.IP = s.GetSocketOption(SocketOptionLevel.IP).
                    e.Command = new Command();//(Command)se.DeserializeObject(commandData, "Command");
                    e.Command.Name = commandData;
                    OnCommandReceived(e);
                }
            }
        }
        public bool TraceEnable { get; set; }
        #endregion

        public List<RemoteClient> Clients { get; set; }

        public event CommandReceiveHandler CommandReceived;
        private void OnCommandReceived(CommandEventArgs e)
        {
            ManageCommands(e.Command);
            if (CommandReceived != null)
            {
                CommandReceived(this, e);
            }

        }
        public event CommandReceiveHandler CommandSended;
        private void OnCommandSended(CommandEventArgs e)
        {
            if (CommandSended != null)
            {
                CommandSended(this, e);
            }
        }
        public void CommandSend(Command e)
        {
            Whe.Serialization.Serializer s = new Whe.Serialization.Serializer();
            s.SerializeObject(e, typeof(Command));
        }
        public void Start()
        {
            Thread tPolicy = new Thread(new ThreadStart(StartPolicy));
            Thread tTrace = new Thread(new ThreadStart(Trace));
            tPolicy.Start();
            tTrace.Start();
        }

        public void ManageCommands(Command c)
        {
            if (c.Name == "GetDownTasks")
            {
                Command rc = new Command();
                rc.Data = "SetDownTasks";
                rc.Data = TaskManager.DownTasks;
                CommandSend(rc);
            }
        }
        
    }
    public delegate void CommandReceiveHandler(object sender, CommandEventArgs e);
    public delegate void CommandSendHandler(object sender, CommandEventArgs e);
    public class RemoteClient
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CommandEventArgs : EventArgs
    {
        public RemoteClient Client { get; set; }
        public Command Command { get; set; }
    }
   
   
}
